package com.huawei.hmsdemo10086.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.huawei.hmsdemo10086.Activity.AddressActivity;
import com.huawei.hmsdemo10086.Activity.Update_AddressActivity;
import com.huawei.hmsdemo10086.POJO.Address;
import com.huawei.hmsdemo10086.R;

import java.util.ArrayList;
import java.util.List;

public class AddressAdapter extends BaseAdapter {

    private List<Address> datas = new ArrayList<Address>();
    private int tag = 0;


    private Context context;
    private LayoutInflater layoutInflater;

    public AddressAdapter(Context context, List<Address> datas) {
        this.datas = datas;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }



    @Override
    public int getCount() {
        return datas.size(); //返回列表的长度
    }

    @Override
    public Address getItem(int position) {
        return datas.get(position); //通过列表的位置 获得集合中的对象
    }

    @Override
    public long getItemId(int position) { // 获得集合的Item的位置
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.layout_address_node, null);//找到布局文件
            convertView.setTag(new ViewHolder(convertView));
        }
        initViews(getItem(position), (ViewHolder) convertView.getTag());
        return convertView;

    }

    private void initViews(Address data, ViewHolder holder) {//初始化数据

        /**
         * 第一次初始话的时候通过 要请求的Url地址 为每个图片设置一个Tag标记,
         * 然后在设置图片的时候判断Tag标记如果是才把图片设置到ImageView上,
         * 这做的原因是为了防止ListView 中的图片错位...
         */
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AddressActivity.tag == 1)
                {
                    Intent intent = new Intent(context, Update_AddressActivity.class);
                    intent.putExtra("address", data.getAddress());
                    intent.putExtra("name", data.getName());
                    intent.putExtra("phone", data.getPhone());
                    intent.putExtra("addressid", ""+data.getAddressid());
                    intent.putExtra("region", ""+data.getRegion());
                    intent.putExtra("tag",1);
                    intent.putExtra("cars",AddressActivity.cars);
                    context.startActivity(intent);
                }
                else
                {
                    Intent intent = new Intent(context, Update_AddressActivity.class);
                    intent.putExtra("address", data.getAddress());
                    intent.putExtra("name", data.getName());
                    intent.putExtra("phone", data.getPhone());
                    intent.putExtra("addressid", ""+data.getAddressid());
                    intent.putExtra("region", ""+data.getRegion());
                    context.startActivity(intent);
                }



            }
        });
        holder.region.setText(data.getRegion());

        holder.address_id.setText(""+data.getAddressid());

        holder.address.setText(data.getAddress());//设置Tag

        //设置新闻标题为集合中获得的标题
        holder.name.setText(data.getName());


        holder.phone.setText(data.getPhone());



    }

    protected class ViewHolder {
        private TextView address;
        private TextView name;
        private TextView phone;
        private Button button;
        private TextView region;
        private TextView address_id;

        public ViewHolder(View view) {
            name = (TextView) view.findViewById(R.id.address_name);
            address = (TextView) view.findViewById(R.id.input_address);
            phone = (TextView) view.findViewById(R.id.address_phone);
            button = (Button)view.findViewById(R.id.change_address);
            address_id = (TextView)view.findViewById(R.id.address_id);
            region = (TextView)view.findViewById(R.id.address_region);
        }
    }

//    public void getImage(Context context, String imgUrl,
//                         final ImageView imageView) {
//
//        /**
//         * 检测图片的Tag值 ,如果根请求的地址相同 才做图片的网络请求.
//         */
//        if (imageView.getTag().toString().equals(imgUrl)) {
//            RequestQueue mQueue = Volley.newRequestQueue(context);
//            ImageRequest imageRequest = new ImageRequest(imgUrl,
//                    new Response.Listener<Bitmap>() {
//                        @Override
//                        public void onResponse(Bitmap response) {
//                            Bitmap outBitmap = RoundRectImageView.getRoundBitmapByShader(response, 500,300,10, 0);
//                            imageView.setImageBitmap(outBitmap);//将返回的Bitmap显示子啊ImageView上
//                        }
//                    }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
//                @Override
//                public void onErrorResponse(VolleyError error) {
//                }
//            });
//            mQueue.add(imageRequest);
//        }
//    }
}
